﻿//-------------------------------------------------------------------------------------------------------------------------------------
// Copyright (C) 2005 greatelsoft.com All Rights Reserved
//-------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Data.Common;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using GTS.Channel.Api.Entity;
using Pub.Class;

namespace GTS.Channel.Api.Helper {
    /// <summary>
    /// 操作类
    /// 
    /// 修改纪录
    ///     2012-04-01 版本：1.0 系统自动创建此类
    /// 
    /// </summary>
    public partial class SPInfoHelper {
        /// <summary>
        /// 缓存多少秒 x 5
        /// </summary>
        public static int cacheSeconds = 1440;
        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <param name="where">附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="order">排序字段，不加“order by”</param>
        /// <param name="fieldList">设置需要返回的字段</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时随机取连接key</param>
        /// <returns>返回实体记录集</returns>
        public static IList<SPInfo> SelectListByAll(string dbkey = "", string where = "", string order = "", string fieldList = "") {
            string cacheNameKey = "SPInfoCache_SelectListByAll_{0}_{1}_{2}".FormatWith(where, order, fieldList);
            return Cache2.Get<IList<SPInfo>>(cacheNameKey, cacheSeconds, () => {
                string strSql = string.Empty;
                if (fieldList.IsNullEmpty()) {
                    strSql = new SQL().From(SPInfo._)
                        .Select(SPInfo._SPCode)
                        .Select(SPInfo._SPPort)
                        .Select(SPInfo._SPDesc)
                        .Select(SPInfo._Status)
                        .Select(SPInfo._ID)
                        .ToString();
                } else {
                    strSql = new SQL().From(SPInfo._).Select(fieldList).ToString();
                }
                if (!where.IsNullEmpty()) strSql += " where 1=1 " + where;
                if (!order.IsNullEmpty()) strSql += " order by " + order;
                return Data.Pool(dbkey).GetDbDataReader(strSql).ToList<SPInfo>();
            });
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <returns>返回实体记录集</returns>
        public static IList<SPInfo> SelectListByAll(string dbkey = "") {
            return SelectListByAll(dbkey, "", "", "");
        }
        /// <summary>
        /// 清除查询所有记录的缓存
        /// </summary>
        public static void ClearCacheSelectListByAll() {
            //Cache2.Remove("SPInfoCache_SelectListByAll___");
            Cache2.RemoveByPattern("SPInfoCache_SelectListByAll_(.+?)");
        }
        /// <summary>
        /// 清除所有缓存
        /// </summary>
        public static void ClearCacheAll() {
            Cache2.RemoveByPattern("SPInfoCache_(.+?)");
        }
    }
}

